var db = {};

var redis = require("redis");

/* 本机开发测试环境 */
// var client = redis.createClient(6379, '192.168.10.77');

/* 腾讯轻量云-云端部署，node容器地址 */
//var client = redis.createClient(6379, '172.17.0.4');

/* 本机 。docker  redis地址 */
//  var client = redis.createClient(6379, '192.168.10.100');

//var client = redis.createClient(6379, '127.0.0.1');

/* 云端 */
// const redisLink = 'redis://124.222.147.172:6379'
const redisLink = 'redis://192.168.10.100:6379'




const pwd = 'zxtkJsbsBbfiasjD156'
const opts = {
	auth_pass: pwd,
  };
var client = redis.createClient(redisLink,opts);





// var client = redis.createClient(6379, '124.222.147.172');



 
client.on("error", function (err) {
  console.log("Error :" , err);
});
 
client.on('connect', function(){
  console.log('Redis连接成功.');
})
 
/**
 * 添加string类型的数据
 * @param key 键
 * @params value 值 
 * @params expire (过期时间,单位秒;可为空，为空表示不过期)
 * @param callBack(err,result)
 */
db.set = function(key, value, expire, callback){
	client.set(key, value, function(err, result){
 
		if (err) {
			console.log(err);
			callback(err,null);
			return;
		}
 
		if (!isNaN(expire) && expire > 0) {
			client.expire(key, parseInt(expire));
		}
 
		callback(null,result)
	})
}
 
/**
 * 查询string类型的数据
 * @param key 键
 * @param callBack(err,result)
 */
db.get = function(key, callback){
 
	client.get(key, function(err,result){
 
		if (err) {
			console.log(err);
			callback(err,null)
			return;
		}
 
		callback(null,result);
	});
}
 
module.exports = db;

